/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Message;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.ProductImage;
import com.hboxs.asl.entity.Question;
import com.hboxs.asl.service.QuestionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Controller - 回答问题
 */
@Controller("adminAnswerController")
@RequestMapping("/asl-admin/answer")
public class AnswerController extends BaseController {

    @Resource(name = "questionServiceImpl")
    private QuestionService questionService;

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model, HttpSession session) {
        session.setAttribute(IMAGE_TYPE , ProductImage.imageType.question);
        model.addAttribute("question", questionService.find(id));
        return "/admin/answer/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Question question) {

        if (!isValid(question)) {
            return ERROR_VIEW;
        }
        question.setAnswerStatus(Question.AnswerStatus.yes);
        questionService.update(question, "member", "teacher", "questionImage", "birth", "gender", "isShare", "name", "payPrice", "payType",
                "birth", "birthHour", "title", "email", "paymentStatus", "images", "mediaURL", "sn");
        return "redirect:list.htm";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model) {
        model.addAttribute("page", questionService.findPageByPayment(pageable));
        return "/admin/answer/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        questionService.delete(ids);
        return SUCCESS_MESSAGE;
    }

}
